﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Universe.TaskBarManager
{
    using System.Diagnostics;
    using System.Threading;
    using System.Windows.Forms;

    using TaskBarManager;

    public class TaskbarStateHelper : IDisposable
    {
        private Control _Control;
        private TaskbarStateKind _kind;

        public TaskbarStateHelper()
        {
        }

        private void Start()
        {
            if (!TaskbarManager.IsPlatformSupported)
                return;

            Trace.WriteLine("TaskBarStateHelper: Start " + _Control.Text + " id=" + _Control.Handle);

            if (_kind == TaskbarStateKind.Error)
                TaskbarManager.Instance.SetProgressValue(100, 100, _Control.Handle);

            TaskbarProgressBarState xxx = (TaskbarProgressBarState)(int)_kind;
            TaskbarManager.Instance.SetProgressState(xxx, _Control.Handle);
        }

        public void SetNormalProgress(int current, int total)
        {
            if (!TaskbarManager.IsPlatformSupported) return;
            TaskbarManager.Instance.SetProgressValue(current, total, _Control.Handle);
            TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal, _Control.Handle);
        }

        public void SetError()
        {
            if (!TaskbarManager.IsPlatformSupported) return;
            TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Error, _Control.Handle);
        }

        public void SetState(TaskbarStateKind kind)
        {
            if (!TaskbarManager.IsPlatformSupported) return;
            TaskbarManager.Instance.SetProgressState((TaskbarProgressBarState)(int)kind, _Control.Handle);
        }

        private delegate void Action();

        public void Stop()
        {
            if (!TaskbarManager.IsPlatformSupported)
                return;

            Trace.WriteLine("TaskBarStateHelper: Stop " + _Control.Text + " id=" + _Control.Handle);

            if (_kind != TaskbarStateKind.Indeterminate)
                TaskbarManager.Instance.SetProgressValue(0, 100, _Control.Handle);

            TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress, _Control.Handle);

            ThreadPool.QueueUserWorkItem(state =>
            {
                Action a = () =>
                {
                    if (_kind != TaskbarStateKind.Indeterminate)
                        TaskbarManager.Instance.SetProgressValue(0, 100, _Control.Handle);

                    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress, _Control.Handle);
                };

                if (_Control != null)
                    _Control.BeginInvoke(a);
            });
        }

        public void Dispose()
        {
            Stop();
        }

        public static TaskbarStateHelper Run(Control control, TaskbarStateKind kind)
        {

            TaskbarStateHelper ret = new TaskbarStateHelper()
            {
                _Control = control,
                _kind = kind,
            };

            ret.Start();
            return ret;
        }
    }

    public enum TaskbarStateKind
    {
        NoProgress = 0,
        Indeterminate = 1,
        Normal = 2,
        Error = 4,
        Paused = 8,
    }

    public static class FormsTaskbarExtentions
    {
        public static TaskbarStateHelper SetTaskbarAs(Form form, TaskbarStateKind kind)
        {
            return TaskbarStateHelper.Run(form, kind);
        }

        public static TaskbarStateHelper SetTaskbarAsError(Form form)
        {
            return TaskbarStateHelper.Run(form, TaskbarStateKind.Error);
        }

        public static TaskbarStateHelper SetTaskbarAsWorking(Form form)
        {
            return TaskbarStateHelper.Run(form, TaskbarStateKind.Indeterminate);
        }
    }
}
